package dev.loong.boot3.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;


@Configuration
@EnableAsync
@ComponentScan("dev.loong.boot3.tasks")
@EnableScheduling
public class AsyncConfig implements AsyncConfigurer {
    /**
     * 创建线程池
     * @return
     */
    @Override
    public Executor getAsyncExecutor() {
        // 创建线程池
        var taskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程数
        taskExecutor.setCorePoolSize(10);
        // 最大线程数
        taskExecutor.setMaxPoolSize(20);
        // 队列容量
        taskExecutor.setQueueCapacity(100);
        // 初始化
        taskExecutor.initialize();
        return taskExecutor;
    }
}
